package com.lsh.payment.core.service.async.impl;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.lsh.payment.core.constant.RedisKeyConstant;
import com.lsh.payment.core.dao.redis.RedisListDao;
import com.lsh.payment.core.dao.redis.RedisStringDao;
import com.lsh.payment.core.model.payasync.CmQueryModel;
import com.lsh.payment.core.model.payasync.MisPurseQueryModel;
import com.lsh.payment.core.model.payasync.QueryModel;
import com.lsh.payment.core.service.async.IEventListener;
import com.lsh.payment.core.service.cmpay.impl.CmQueryServiceImpl;
import com.lsh.payment.core.service.mispurse.impl.PurseQueryServiceImpl;
import com.lsh.payment.core.service.qfpay.impl.QfQueryServiceImpl;
import org.jsoup.helper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

/**
 * Project Name: lsh-payment
 * @author by jingyuan
 * Date: 16/12/22
 * 北京链商电子商务有限公司
 * Package com.lsh.payment.core.service.AsyncService
 * desc:异步处理监听类
 */
@Component
public class QueryEventListener implements IEventListener {
    private Logger logger = LoggerFactory.getLogger(QueryEventListener.class);

    @Autowired
    private RedisStringDao redisStringDao;

    @Autowired
    private RedisListDao redisListDao;

    @Value("${qf.query.timeout}")
    private long qfQueryTimeout;

    @Autowired
    private QfQueryServiceImpl qfQueryService;

    @Autowired
    private PurseQueryServiceImpl purseQueryService;

    @Autowired
    private CmQueryServiceImpl cmQueryService;


    /**
     * 钱方查询
     *
     * @param queryModel 监听类
     */
    @Subscribe
    @AllowConcurrentEvents
    public void qfQuery(QueryModel queryModel) {
        logger.info("钱方异步查询开始");
        //5s一次
        String qfQuerykey = MessageFormat.format(RedisKeyConstant.QF_QUERY_KEY, queryModel.getPayDeal().getChannelTransaction());
        String qfQueryLock = redisStringDao.get(qfQuerykey);
        if (StringUtil.isBlank(qfQueryLock)) {
            redisStringDao.set(qfQuerykey, "1");
            redisListDao.expire(qfQuerykey, qfQueryTimeout);
            qfQueryService.query(queryModel.getPayDeal());
        }
        logger.info("钱方异步查询结束");
    }

    /**
     * mis异步查询
     *
     * @param misPurseQueryModel 监听类
     */
    @Subscribe
    @AllowConcurrentEvents
    public void MisPurseQuery(MisPurseQueryModel misPurseQueryModel) {
        logger.info("mis异步查询开始");
        purseQueryService.query(misPurseQueryModel.getPayDeal());
        logger.info("mis异步查询结束");
    }

    /**
     * mis异步查询
     *
     * @param cmQueryModel 监听类
     */
    @Subscribe
    @AllowConcurrentEvents
    public void cmPurseQuery(CmQueryModel cmQueryModel) {
        logger.info("mis异步查询开始");
        cmQueryService.query(cmQueryModel.getPayDeal());
        logger.info("mis异步查询结束");
    }


}
